<?php

namespace app\models\base;


use app\models\Helper\Url;
use \app\models\member\MemberAccount;
use app\models\member\MemberOther;

/**
 * Login form
 */
class LoginForm extends \app\api\components\ApiModel
{

    public $username;
    public $password;
    private $_user;
    public $other_id = '';
    public $type;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['username', 'password'], 'required'],
            // rememberMe must be a boolean value
            ['other_id', 'string'],
            ['type', 'integer'],
            // password is validated by validatePassword()
            // username and password are both required
            ['password', 'validatePassword'],

        ];
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validatePassword($attribute, $params)
    {

        if (!$this->hasErrors()) {
            $user = $this->getUser();
            if (!$user || !$user->validatePassword($this->password)) {
                $this->addError($attribute, '用户名或密码不正确');
            }
        }
    }

    /**
     * Logs in a user using the provided username and password.
     *
     */
    public function login()
    {
        if ($this->validate()) {
            $user = $this->getUser();
            if ($user) {

                $user->last_login_time = time();
                $token = md5(sha1($user->login_account . $user->login_password . time()));
                $user->token = $token;
                $user->status = $user::LOGIN_TRUE;
                $res = $user->save();
                if (!$res) {
                    return false;
                }
                $cache = \Yii::$app->cache;
                $cache['token' . $user->user_id] = $token;
                $userInfo = \app\models\member\MembersInfo::findOne($user->user_id);


                $data = [
                    'user_id' => $user['user_id'],
                    'mobile' => $user['login_account'],
                    'email' => $userInfo['email'],
                    'nickname' => $userInfo['name'],
                    'avatar' => Url::img($userInfo['avatar']),
                    'sex' => $userInfo['sex'],
                    'token' => $token,
                    'authorize_status' => $userInfo['active'],
                    'create_time' => $user['create_time'],
                    'last_login_time' => $user['last_login_time'],
                ];

                $bind_info = Url::isBindOther($user->user_id);
                $data['is_bind_sina'] = $bind_info['is_bind_sina'];
                $data['is_bind_weixin'] = $bind_info['is_bind_weixin'];
                $data['is_bind_qq'] = $bind_info['is_bind_qq'];

                //第三方登陆保存第三方信息
                if ($this->other_id) {
                    //var_dump($this->type);exit;
                    //判断是否绑定此第三方信息
                    $other = MemberOther::findOne(['other_id' => $this->other_id, 'type' => $this->type, 'user_id' => $user->user_id]);
                    $other1 = MemberOther::findOne(['other_id' => $this->other_id, 'type' => $this->type]);
                    $other2 = MemberOther::findOne(['user_id' => $user->user_id, 'type' => $this->type]);
                    if ($other) {//判断当前第三方信息是否被当前用户绑定
                        return $data;
                    } elseif ($other1) {//判断第三方信息是否绑定
                        return -1;
                    } elseif ($other2) {//判断用户是否绑定此类型的第三方信息
                        return -2;
                    } else {
                        $model = new MemberOther();
                        $model->other_id = $this->other_id;
                        $model->type = $this->type;
                        $model->user_id = $user->user_id;
                        $model->create_time = time();
                        $res = $model->save();
                        if ($res) {
                            $bind_info = Url::isBindOther($user->user_id);
                            $data['is_bind_sina'] = $bind_info['is_bind_sina'];
                            $data['is_bind_weixin'] = $bind_info['is_bind_weixin'];
                            $data['is_bind_qq'] = $bind_info['is_bind_qq'];
                            //var_dump($data);exit();
                            return $data;
                        } else {
                            return false;
                        }
                    }
                } else {
                    return $data;
                }
            }
            return false;
        } else {
            return false;
        }
    }

    /**
     * Finds user by [[username]]
     *
     * @return MemberAccount|null
     */
    protected function getUser()
    {
        if ($this->_user === null) {
            $this->_user = MemberAccount::getMemberByAccount($this->username);
        }
        return $this->_user;
    }

}
